package com.oisix.oisystemts2.service.autotask;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Hibernate;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.CriteriaSpecification;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.oisix.common.TestData;
import com.oisix.common.TestEnvironment;
import com.oisix.oisystemec.dao.KokyakuDao;
import com.oisix.oisystemec.model.AutoTaskHistory;
import com.oisix.oisystemec.model.Kokyaku;
import com.oisix.oisystemfr.persistence.EntityManager;
import com.oisix.oisystemfr.persistence.EntityManagerFactory;

public class PointCsvTest {
    private static TestData testData;

    @BeforeClass
    public static void prepare() {
        TestEnvironment.prepare();
        testData = new TestData(
                "src/com/oisix/oisystemts2/service/autotask/PointCsvTest.xls");
        testData.load();
    }

    /**
     * 顧客ログイン解除フラグ
     */

    @Test
    public void testCreateCSV1() {
        PointBalanceService pointBalanceService = new PointBalanceService();
        AutoTaskHistory history = new AutoTaskHistory();
        String userId = "9000";
        pointBalanceService.execute(history, userId);

    }

    @SuppressWarnings("unchecked")
    @Test
    public void testKokyakuDaoGetPointBalance1() {

        List<Map<String, Object>> pointBalanceList = KokyakuDao
                .getPointBalance();

        // Verify
        assertThat(pointBalanceList.size(), is(0));

       
        Kokyaku kokyaku = new Kokyaku();
        kokyaku.setKokyakukubun(new BigDecimal(2));

        EntityManager em = EntityManagerFactory.getEntityManager();
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        StringBuilder sb = new StringBuilder();

        sb.append("select kokyaku_id, shimei , furigana from m_kokyaku order by kokyakuId");

        SQLQuery query = em.createSQLQuery(sb.toString());
        query.addScalar("kokyaku_id", Hibernate.STRING);
        query.addScalar("shimei", Hibernate.STRING);
        query.addScalar("furigana", Hibernate.STRING);

        result = query.setResultTransformer(
                CriteriaSpecification.ALIAS_TO_ENTITY_MAP).list();

        assertThat(result.size(), is(1));
        assertThat((String) result.get(0).get("kokyaku_id"), is("01000001"));
        assertThat((String) result.get(0).get("shimei"), is("追六 太郎1"));
        assertThat((String) result.get(0).get("pointo"), is("0"));
        assertThat((String) result.get(0).get("furigana"), is("オイロク タロウイチ"));


    
    }

    @AfterClass
    public static void cleanUp() {
        testData.cleanup();
        testData = null;
    }
}